import Vue from 'vue';
import Router from 'vue-router';
import store from '@/store/index'
//使用router中间件  
Vue.use(Router);
export const routes = [

  // 路由重定向
  {
    path: '/', redirect: '/home',
  },
  //主页home
  {
    path: '/home',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/index/Home'),
    children: [{
      path: 'detail',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/index/children/Detail')
    },]
  },
  //商品详情
  {
    path: '/list/:productId',
    name: '商品详情',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/index/children/List')
  },

  //分类
  {
    path: '/type',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/type/Index'),
  },
  //购物车
  {
    path: '/cart',
    meta: {
      title: '每日生鲜',
      needLogin: true
    },
    component: () =>
      import('../views/cart/Cart'),
    children: [
      {
        path: 'edit',
        name: '购物车',
        meta: {
          title: '每日生鲜',
          needLogin: true
        },
        component: () =>
          import('../views/cart/children/Edit')
      },

    ],

  },
  //我的、退出
  {
    path: '/my',
    meta: {
      title: '每日生鲜',
      // needLogin:true
    },
    component: () =>
      import('../views/my/Center'),
    children: [{
      path: 'set',
      meta: {
        title: '每日生鲜',
        needLogin: true
      },
      component: () =>
        import('../views/my/children/Set')
    },
    {
      path: 'sign',
      meta: {
        title: '每日生鲜',
        // needLogin:true
      },
      component: () =>
        import('../views/my/children/Sign')
    }
    ],
  },
  //订单order
  {
    path: '/order',
    meta: {
      title: '每日生鲜',
      needLogin: true
    },
    component: () =>
      import('../views/order/Order'),
    children: [{
      path: 'list',
      meta: {
        title: '每日生鲜',
        needLogin: true
      },
      component: () =>
        import('../views/order/chilren/List')
    },
    {
      path: 'detail/:id', 
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Detail')
    },
    {
      path: 'shared',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Shared')
    },
    {
      path: 'goods',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Goods')
    },
    {
      path: 'consignment',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Consignment')
    },
    {
      path: 'pay',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Pay')
    },


    {
      path: 'submit/:id/:radio',
      meta: {
        title: '每日生鲜',
        needLogin: true
      },
      name: '立即购买',
      component: () =>
        import('../views/order/chilren/Submit')
    },
    {
      path: 'payment/:radio/:money/:orderId/:cartId/:id',
      name: '确认付款',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Payment')
    },
    {
      path: 'coupon',
      meta: {
        title: '每日生鲜',
        //needLogin:true
      },
      component: () =>
        import('../views/order/chilren/Coupon')
    },
    ]
  },
  //地址address
  {
    path: '/address',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/address/Conter'),
    children: [

      {
        path: 'list/:id',
        meta: {
          title: '每日生鲜',
          //needLogin:true
        },
        component: () =>
          import('../views/address/chiren/List')
      },
      {
        path: 'edit/:id',
        meta: {
          title: '每日生鲜',
          //needLogin:true
        },
        component: () =>
          import('../views/address/chiren/Edit')
      },
      {
        path: 'newEdit',
        meta: {
          title: '每日生鲜',
          //needLogin:true
        },
        component: () =>
          import('../views/address/chiren/NewEdit')
      }
    ]
  },
  //注册
  {
    path: '/register',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/account/Register')
  },
  //登陆
  {
    path: '/login',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/account/Login')
  },
  //404
  {
    path: '/notFound',
    meta: {
      title: '每日生鲜',
      //needLogin:true
    },
    component: () =>
      import('../views/address/chiren/Edit')
  },
]





let router = new Router({
  scrollBehavior: () => ({
    y: 0
  }),
  routes
})

router.beforeEach((to, form, next) => {
  let islogin = store.state.islogin
  // 修改网页标题
  document.title = to.meta.title;
  // 按需登录
  if (to.meta.needLogin && !islogin) {
    next({ path: '/login' });
  } else {
    next();
  }
})

export default router
